#include "../mtcp_module.h"


EMCT::EMCT()
{
	memset(&m_EMCT_REQ, 0, sizeof(m_EMCT_REQ));
	m_EMCT_REQ.I_DR = 1.2;
}


EMCT::~EMCT()
{
}

int EMCT::IMG_WIDTH(unsigned short v)
{
	m_EMCT_REQ.IMG_WIDTH = v;
	return 0;
}
int EMCT::IMG_HEIGHT(unsigned short v)
{
	m_EMCT_REQ.IMG_HEIGHT = v;
	return 0;
}
int EMCT::IMG_SIZE(unsigned int v)
{
	m_EMCT_REQ.IMG_SIZE = v;
	return 0;
}
int EMCT::NTC_STEADY_STATE(float v)
{
	m_EMCT_REQ.NTC_STEADY_STATE = v;
	return 0;
}
int EMCT::CLUSTER_THRESHOLD(float v)
{
	m_EMCT_REQ.CLUSTER_THRESHOLD = v;
	return 0;
}
int EMCT::VALID_EMITTER_CNT(unsigned char v)
{
	m_EMCT_REQ.VALID_EMITTER_CNT = v;
	return 0;
}
int EMCT::EMITTER_CENTER_X(const char * data)
{
	char * p = new char[strlen(data) + 1];
	strcpy(p, data);

	char * dd;
	char * k = strtok(p, ",");
	int index = 0;
	while (k)
	{
		printf("get : %s\r\n", k);
		m_EMCT_REQ.EMITTER_CENTER_X[index++] = atoi(k);
		if (index >= 256) break;
		k = strtok(NULL, ",");
	}
	return 0;
}
int EMCT::EMITTER_CENTER_Y(const char * data)
{
	char * p = new char[strlen(data) + 1];
	strcpy(p, data);

	char * dd;
	char * k = strtok(p, ",");
	int index = 0;
	while (k)
	{
		printf("get : %s\r\n", k);
		m_EMCT_REQ.EMITTER_CENTER_Y[index++] = atoi(k);
		if (index >= 256) break;
		k = strtok(NULL, ",");
	}
	return 0;
}
int EMCT::EMITTER_SIZE(const char * data)
{
	char * p = new char[strlen(data) + 1];
	strcpy(p, data);

	char * dd;
	char * k = strtok(p, ",");
	int index = 0;
	while (k)
	{
		printf("get : %s\r\n", k);
		m_EMCT_REQ.EMITTER_SIZE[index++] = atoi(k);
		if (index >= 256) break;
		k = strtok(NULL, ",");
	}
	return 0;
}
int EMCT::MAX_POWER(const char * data)
{
	char * p = new char[strlen(data) + 1];
	strcpy(p, data);

	char * dd;
	char * k = strtok(p, ",");
	int index = 0;
	while (k)
	{
		printf("get : %s\r\n", k);
		m_EMCT_REQ.MAX_POWER[index++] = atof(k);
		if (index >= 256) break;
		k = strtok(NULL, ",");
	}
	return 0;
	return 0;
}
int EMCT::MIN_POWER(const char * data)
{
	char * p = new char[strlen(data) + 1];
	strcpy(p, data);

	char * dd;
	char * k = strtok(p, ",");
	int index = 0;
	while (k)
	{
		printf("get : %s\r\n", k);
		m_EMCT_REQ.MIN_POWER[index++] = atof(k);
		if (index >= 256) break;
		k = strtok(NULL, ",");
	}
	return 0;
}
int EMCT::AVG_POWER(const char * data)
{
	char * p = new char[strlen(data) + 1];
	strcpy(p, data);

	char * dd;
	char * k = strtok(p, ",");
	int index = 0;
	while (k)
	{
		printf("get : %s\r\n", k);
		m_EMCT_REQ.AVG_POWER[index++] = atof(k);
		if (index >= 256) break;
		k = strtok(NULL, ",");
	}
	return 0;
}
int EMCT::STD_POWER(const char * data)
{
	char * p = new char[strlen(data) + 1];
	strcpy(p, data);

	char * dd;
	char * k = strtok(p, ",");
	int index = 0;
	while (k)
	{
		printf("get : %s\r\n", k);
		m_EMCT_REQ.STD_POWER[index++] = atof(k);
		if (index >= 256) break;
		k = strtok(NULL, ",");
	}
	return 0;
}
int EMCT::I_DR(float v)
{
	m_EMCT_REQ.I_DR = v;
	return 0;
}
int EMCT::V_FOR(float v)
{
	m_EMCT_REQ.V_FOR = v;
	return 0;
}

int EMCT::setvalue(char *what, char* value)
{
	if (0 == strcmp(what, "IMG_WIDTH")){
		IMG_WIDTH(atoi(value));
		return 0;
	}
	if (0 == strcmp(what, "IMG_HEIGHT")){
		IMG_HEIGHT(atoi(value));
		return 0;
	}
	if (0 == strcmp(what, "IMG_SIZE")){
		IMG_SIZE(atoi(value));
		return 0;
	}
	if (0 == strcmp(what, "NTC_STEADY_STATE")){
		NTC_STEADY_STATE(atof(value));
		return 0;
	}
	if (0 == strcmp(what, "CLUSTER_THRESHOLD")){
		CLUSTER_THRESHOLD(atof(value));
		return 0;
	}
	if (0 == strcmp(what, "VALID_EMITTER_CNT")){
		VALID_EMITTER_CNT(*value);
		return 0;
	}
	if (0 == strcmp(what, "EMITTER_CENTER_X")){
		EMITTER_CENTER_X(value);
		return 0;
	}
	if (0 == strcmp(what, "EMITTER_CENTER_Y")){
		EMITTER_CENTER_Y(value);
		return 0;
	}
	if (0 == strcmp(what, "EMITTER_SIZE")){
		EMITTER_SIZE(value);
		return 0;
	}
	if (0 == strcmp(what, "MAX_POWER")){
		MAX_POWER(value);
		return 0;
	}
	if (0 == strcmp(what, "MIN_POWER")){
		MIN_POWER(value);
		return 0;
	}
	if (0 == strcmp(what, "AVG_POWER")){
		AVG_POWER(value);
		return 0;
	}
	if (0 == strcmp(what, "STD_POWER")){
		STD_POWER(value);
		return 0;
	}
	if (0 == strcmp(what, "I_DR")){
		I_DR(atof(value));
		return 0;
	}
	if (0 == strcmp(what, "V_FOR")){
		V_FOR(atof(value));
		return 0;
	}


	return -1;
}
int EMCT::SendEMCT()
{
	int ret = SendFrame(kMTCP_CTRL_EMCT, &m_EMCT_REQ, sizeof(m_EMCT_REQ), nullptr, 0);
	tMTCP_payload_TEST_RSP rep;
	int recv_cnt = RecvResponse(&rep, sizeof(rep));
	return ret;
}